package com.trt.beibei.api.v1;

import com.trt.beibei.core.LocalUser;
import com.trt.beibei.core.UnifyResponse;
import com.trt.beibei.core.enumeration.CouponStatus;
import com.trt.beibei.core.interceptors.ScopeLevel;
import com.trt.beibei.exception.http.ParameterException;
import com.trt.beibei.model.Coupon;
import com.trt.beibei.model.User;
import com.trt.beibei.service.CouponService;
import com.trt.beibei.vo.CouponCategoryVO;
import com.trt.beibei.vo.CouponPureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tanrentao
 * @date 2021-08-13
 * @desc
 */
@RestController
@RequestMapping("coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * 通过分类id查询优惠卷
     *
     * @param cid
     */
    @GetMapping("/by/category/{cid}")
    public List<CouponPureVO> getCouponListByCategory(@PathVariable Long cid) {
        List<Coupon> coupons = couponService.getByCategory(cid);
        if (coupons.isEmpty()) {
            return Collections.emptyList();
        }
        List<CouponPureVO> couponPureVOS = CouponPureVO.getList(coupons);
        return couponPureVOS;
    }

    /**
     *
     * 获取全场券
     * @param
     */
    @GetMapping("/whole_store")
    public List<CouponPureVO> getWholeStoreCouponList() {
        List<Coupon> coupons = couponService.getWholeStoreCoupons();
        if (coupons.isEmpty()) {
            return Collections.emptyList();
        }
        return CouponPureVO.getList(coupons);
    }

    // 用户领取优惠卷
    @ScopeLevel()
    @PostMapping("/collect/{id}")
    public void collectCoupon(@PathVariable Long id) {
        // 朝权问题
        // 用户id不能通过参数形式显示的传递到服务端 /collect/{id}/user/{uid}
        // 涉及到超权,这里超权是用户可以通过该uid实现自己的令牌获取到其他用户的数据
        // 解决：从令牌中获取
        Long uid = LocalUser.getUser().getId();
        couponService.collectOneCoupon(uid, id);
        UnifyResponse.createSuccess(0);
    }

    // 根据状态获取优惠卷
    @ScopeLevel
    @GetMapping("/myself/by/status/{status}")
    public List<CouponPureVO> getMyCouponByStatus(@PathVariable Integer status) {
        Long uid = LocalUser.getUser().getId();
        List<Coupon> couponList;

        // status不能准确表示优惠券状态，因为引入了延时订单支付 2 —> 1(不一定能100%保证状态2->1)  异步机制  事务 解决 数据不一致性 (异步机制下的数据一致性不一定能100%保证)

        //触发机制 时机 过期
        switch (CouponStatus.toType(status)) {
            case AVAILABLE:
                couponList = couponService.getMyAvailableCoupons(uid);
                break;
            case USED:
                couponList = couponService.getMyUsedCoupons(uid);
                break;
            case EXPIRED:
                couponList = couponService.getMyExpiredCoupons(uid);
                break;
            default:
                throw new ParameterException(40001);
        }
        return CouponPureVO.getList(couponList);
    }

    // 查询我的可用优惠券
    @ScopeLevel()
    @GetMapping("/myself/available/with_category")
    public List<CouponCategoryVO> getUserCouponWithCategory() {
        User user = LocalUser.getUser();
        List<Coupon> coupons = couponService.getMyAvailableCoupons(user.getId());
        if (coupons.isEmpty()) {
            return Collections.emptyList();
        }
        return coupons.stream().map(coupon -> {
            CouponCategoryVO vo = new CouponCategoryVO(coupon);
            return vo;
        }).collect(Collectors.toList());
    }


}
